package vip.xiaonuo.smzq.modular.user.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import vip.xiaonuo.auth.core.pojo.SaBaseLoginUser;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.smzq.modular.kcl.mapper.KsMapper;
import vip.xiaonuo.smzq.modular.user.entity.Kyqrqyb;
import vip.xiaonuo.smzq.modular.user.mapper.KyqrqybMapper;
import vip.xiaonuo.smzq.modular.user.service.IKyqrqybService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.page.CommonPageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 矿业权人企业表 服务实现类
 *
 * @author sqq
 * @since 2024-07-20
 */
@Service
public class KyqrqybServiceImpl extends ServiceImpl<KyqrqybMapper, Kyqrqyb> implements IKyqrqybService {

    @Resource
    private KyqrqybMapper kyqrqybMapper;


    @Override
    public Page<Kyqrqyb> page(Kyqrqyb kyqrqyb) {
        QueryWrapper<Kyqrqyb> queryWrapper = new QueryWrapper<>();
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    /**
     * 同步矿业权人企业表
     */
    @Override
    public void synchronizationKyqrqyb() {
        //获取用户信息
        SaBaseLoginUser saBaseLoginUser = StpLoginUserUtil.getLoginUser();
        List<String> list = kyqrqybMapper.getKyqrqyb();
        for (String str : list){
            // 使用逗号作为分隔符进行分割
            String[] parts = str.split(",");
            Kyqrqyb kyqrqyb = new Kyqrqyb();
            kyqrqyb.setKqrssgs(parts[0]);
            kyqrqyb.setZjbh(parts[1]);
            kyqrqyb.setZjlx("统一社会信用代码");
            kyqrqyb.setCzrid(saBaseLoginUser.getId());
            kyqrqyb.setCzrxm(saBaseLoginUser.getName());
            kyqrqyb.setCzsj(DateUtil.format(new Date(), "YYYY-MM-dd"));
            kyqrqyb.setDelflag(0);
            kyqrqybMapper.insert(kyqrqyb);

        }
    }


    /**
     * 矿业权人企业表分页查询
     * @param kqrssgs 矿权人所属公司
     */
    @Override
    public Page<Kyqrqyb> getPage(String kqrssgs) {
        Page<Kyqrqyb> page = new Page<>();
        page.setSize(20);
        page.setCurrent(1);
        LambdaQueryWrapper<Kyqrqyb> queryWrapper = new LambdaQueryWrapper<>();
        if (kqrssgs != null&& !kqrssgs.equals("")){
            queryWrapper.like(Kyqrqyb::getKqrssgs, kqrssgs);
        }
        queryWrapper.eq(Kyqrqyb::getDelflag, 0);
        return kyqrqybMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<String> getCompany(String kqrssgs) {
        Page<Kyqrqyb> page =   getPage(kqrssgs);
        List<Kyqrqyb> records = page.getRecords();
        List<String> kqrssgsList = new ArrayList<>();
        if (records.size() > 0){
            for (Kyqrqyb kyqrqyb : records){
                kqrssgsList.add(kyqrqyb.getKqrssgs());
            }
        }
        return kqrssgsList;
    }
}
